package cn.ming.wfchat.app.moment;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import cn.ming.wfchat.R;
import cn.ming.wfchat.app.main.DiscoveryFragment;
import cn.ming.wfchat.kit.widget.InputAwareLayout;
import cn.ming.wfchat.kit.widget.KeyboardAwareLinearLayout;


public class CommentFragment extends Fragment implements KeyboardAwareLinearLayout.OnKeyboardHiddenListener,
        KeyboardAwareLinearLayout.OnKeyboardShownListener {
    private InputAwareLayout inputAwareLayout;
    private FeedCommentPanel commentInputPanel;
    private EditText editText;

    private String hash;
    private String commentId;

    private int commentPanelTop = 0;
    private KeyboardAwareLinearLayout.OnKeyboardHiddenListener onKeyboardHiddenListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            hash = bundle.getString("hash");
            commentId = bundle.getString("commentId");
        }
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.comment_fragment, container, false);
        inputAwareLayout = view.findViewById(R.id.rootInputAwareLayout);
        inputAwareLayout.addOnKeyboardHiddenListener(this);
        inputAwareLayout.addOnKeyboardShownListener(this);
        commentInputPanel = view.findViewById(R.id.commentPanel);

        commentInputPanel.init(this, inputAwareLayout);
        editText = commentInputPanel.editText;
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inputAwareLayout.showSoftkey(editText);
        commentPanelTop = PreferenceManager.getDefaultSharedPreferences(getActivity()).getInt("commentPanelTop", 0);
    }

    @Override
    public void onKeyboardShown() {
        commentInputPanel.onKeyboardShown();
        if (commentPanelTop > 0) {
            return;
        }

        commentInputPanel.post(() -> {
            int[] location = new int[2];
            commentInputPanel.getLocationInWindow(location);
            PreferenceManager.getDefaultSharedPreferences(getActivity())
                    .edit()
                    .putInt("commentPanelTop", location[1])
                    .apply();
        });
    }

    public void comment(String comment) {
        DiscoveryFragment discoveryFragment = (DiscoveryFragment) getParentFragment();
        if (discoveryFragment == null) {
            return;
        }
        if (!TextUtils.isEmpty(hash)) {
            if (!TextUtils.isEmpty(commentId)) {
                discoveryFragment.replyComment(hash, commentId, comment);
            } else {
                discoveryFragment.commentFeed(hash, comment);
            }
        }

    }

    public void hideKeyboard() {
        inputAwareLayout.hideSoftkey(editText, null);
    }

    @Override
    public void onKeyboardHidden() {
        commentInputPanel.onKeyboardHidden();
    }

//    @Override
//    public void onKeyboardHidden() {
//        commentInputPanel.onKeyboardHidden();
//    }
}
